package com.edu.whty.service.base.impl;

import com.edu.whty.dao.base.CommonPraiseDao;
import com.edu.whty.dao.base.UserExamFlagDao;
import com.edu.whty.entity.base.CommonPraise;
import com.edu.whty.entity.base.UserExamFlag;
import com.edu.whty.service.base.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("commonService")
public class CommonServiceImpl implements CommonService {
	@Autowired
    private CommonPraiseDao commonPraiseDao;
	@Autowired
	private UserExamFlagDao userExamFlagDao;
	
	@Override
	public void updateAvgScore(String businessType, String businessId,
			int newScore, String periodId, String subjectId,String userId,String netIp) throws Exception {
		
		/*精品习题需要传periodId，subjectId两个参数，题库接口需要
		if (!BUSINESS_TYPE_EXCLLENT_QUESTION.equals(businessType)) {
			if(BUSINESS_TYPE_SHARE_PREPARATION.equals(businessType)){
				updateAvgScore(businessType, businessId, newScore,netIp);
			}else{
				updateAvgScore(businessType, businessId, newScore,userId,netIp);
			}
		}*/
	}

	@Override
	public void addPraise(CommonPraise p) throws Exception {
		p.setId(UUID.randomUUID().toString());
		p.setPraiseTime(new Date());
		commonPraiseDao.save(p);
	}

	@Override
	public void deletePraise(CommonPraise p) throws Exception {
		commonPraiseDao.cancelPraise(p);
	}

	@Override
	public boolean hasPraise(Map<String, Object> param) throws Exception {
		List<CommonPraise> praiseList=commonPraiseDao.listByCondition(param);
		return (praiseList!=null && !praiseList.isEmpty());
	}
	@Override
	public Integer getPraiseCount(CommonPraise p) throws Exception {
		return commonPraiseDao.selectByConditionCount(p);
	}
	/**
	 * @category 查询点赞数量
	 * @param param
	 * @throws Exception
	 */
	@Override
	public Integer getPraiseCountClass(CommonPraise p)throws Exception{
		return commonPraiseDao.getPraiseCountClass(p);
	}
	@Override
	public List<CommonPraise> praiseDetail(Map<String, Object> param) throws Exception {
		List<CommonPraise> praiseList=commonPraiseDao.listByCondition(param);
		return praiseList;
	}

	@Override
	public UserExamFlag getUserExamFlag(UserExamFlag examFlag) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("userId", examFlag.getUserId());
		queryMap.put("loginPlatformCode", examFlag.getLoginPlatformCode());
		return userExamFlagDao.getExamFlagByUserId(queryMap);
	}

	@Override
	public UserExamFlag updateUserExamFlag(UserExamFlag examFlag) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("userId", examFlag.getUserId());
		queryMap.put("loginPlatformCode", examFlag.getLoginPlatformCode());
		Date now = new Date();
		if(null == userExamFlagDao.getExamFlagByUserId(queryMap)) {
			//新增
			examFlag.setCreateTime(now);
			examFlag.setUpdateTime(now);
			userExamFlagDao.insertExamFlag(examFlag);
		}else {
			//更新
			examFlag.setUpdateTime(now);
			userExamFlagDao.updateExamFlagByUserId(examFlag);
		}
		return examFlag;
	}
	
}
